package com.superm.controller;

import com.superm.entity.Brand;
import com.superm.service.BrandService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 品牌控制器
 */
@Controller
@RequestMapping("/brand")
public class BrandController {

    //品牌接口
    @Autowired
    private BrandService brandService;

    /**
     * 跳转到品牌展示页面
     * @return
     */
    @RequestMapping("/skipBrand")
    public ModelAndView skipBrand(){
        return new ModelAndView("brand/brandList");
    }

    /**
     * 跳转到修改品牌页面
     * @return
     */
    @RequestMapping("/skipUpdateBrand")
    public ModelAndView skipUpdateBrand(@Param("brandId") Integer brandId){
        ModelAndView mv = new ModelAndView("brand/updateBrand");
        //查询指定编号的指定信息
        Brand brand = brandService.selectIdBrand(brandId);
        mv.addObject("brand",brand);
        return mv;
    }

    /**
     * 跳转到修改品牌页面
     * @return
     */
    @RequestMapping("/skipAddBrand")
    public ModelAndView skipAddBrand(){
        ModelAndView mv = new ModelAndView("brand/addBrand");
        return mv;
    }
    /**
     * 模糊查询品牌信息
     * @param brand
     * @return
     */
    @RequestMapping("/selectLikeBrand")
    @ResponseBody
    public Map<String,Object> selectLikeBrand(Brand brand, @RequestParam("page")Integer pageNo,@RequestParam("limit")Integer pageSize){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        //查询条数
        int count = brandService.selectBrandCount(brand);
        map.put("count",count);
        //查询所有信息品牌信息
        List<Brand> listBrand = brandService.selectLikeBrand(brand,pageNo,pageSize);
        map.put("data",listBrand);
        return map;
    }

    /**
     *修改品牌信息
     * @param brand
     * @return
     */
    @RequestMapping("updateBrand")
    @ResponseBody
    public Map<String,Object> updateBrand(Brand brand){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (brandService.updateBrand(brand)>0){
            map.put("msg","修改成功!");
            map.put("success",true);
        }

        return  map;
    }





    /**
     *添加品牌信息
     * @param brand
     * @return
     */
    @RequestMapping("addBrand")
    @ResponseBody
    public Map<String,Object> addBrand(Brand brand){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (brandService.addBrand(brand)>0){
            map.put("msg","添加成功!");
            map.put("success",true);
        }

        return  map;
    }

    /**
     *添加品牌信息
     * @param brandId
     * @return
     */
    @RequestMapping("deleteBrand")
    @ResponseBody
    public Map<String,Object> deleteBrand(Integer brandId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        if (brandService.deleteBrand(brandId)>0){
            map.put("msg","删除成功!");
            map.put("success",true);
        }
        return  map;
    }

}
